package com.emser.unc.server;

import java.util.ArrayList;

import com.emser.unc.client.service.SellerService;
import com.emser.unc.serializable.LocationDTO;
import com.emser.unc.serializable.ResultDTO;
import com.emser.unc.serializable.SellerDTO;
import com.emser.unc.server.domain.Device;
import com.emser.unc.shared.exception.NotLoggedInException;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class SellerServiceImpl extends RemoteServiceServlet implements SellerService{
	@Override
	public boolean addSeller(String userAName,SellerDTO seller)throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibSeller.addSellerDataStore(userAName,seller);
		}
	}

	@Override
	public ResultDTO<SellerDTO> getSellers(String cursorString, int range,String userAName) throws NotLoggedInException{
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibSeller.getSellersDataStore(cursorString, range,userAName);
		}
	}

	@Override
	public boolean deleteSeller(String userAName, String sellerName)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibSeller.deleteSellerDataStore(userAName, sellerName);
		}
	}

	@Override
	public boolean updateSeller(String userAName,SellerDTO seller) throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibSeller.updateSellerDataStore(seller);	
		}	
	}
	
	@Override
	public Integer getSellersCount(String userAName) throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibSeller.getSellersCount(userAName);
		}
	}
	
	@Override
	public ResultDTO<LocationDTO> getSerialDevices(String cursorString, int range,String userAName)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibDevices.getSerialDevices(cursorString, range,userAName);
		}
	}

	@Override
	public ResultDTO<SellerDTO> getSearchSellers(String cursorString, int range, ArrayList<String> searchParam) throws NotLoggedInException {	
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibSeller.searchSellers(cursorString,range,searchParam);
		}
	}

	@Override
	public Integer getSResultsCount(ArrayList<String> searchParam)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibSeller.getSearchResultsCount(searchParam);
		}
	}

	@Override
	public SellerDTO getSeller(String userNSeller, String userAName)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibSeller.getSellerDTO(userNSeller);
		}
	}

	@Override
	public Integer getSerialDevicesCount(String userAName)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibDevices.getSerialDevicesCount(userAName);
		}
	}

	@Override
	public LocationDTO getLocation(String serialDevice, String userAName)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return Device.toDTO(AppLibDevices.getLocation(serialDevice));
		}
		
	}

	
}
